package com.barbeque.mall.controller.test;

import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.system.MarketRoleOptions;
import com.barbeque.mall.bean.system.bo.FirstClass;
import com.barbeque.mall.bean.system.bo.RoleOptionsTest;
import com.barbeque.mall.bean.system.bo.SecondClass;
import com.barbeque.mall.bean.system.bo.ThirdClass;
import com.barbeque.mall.service.test.RoleTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/18 23:58
 */
@RestController
@RequestMapping("admin/role")
public class RoleOptionsTestController {

    @Autowired
    RoleTestService roleTestService;
    @RequestMapping("test")
    public BaseRespVo roleTest(@RequestBody RoleOptionsTest roleOptionsTest){
        int l1 = 0;
        int l2 = 0;
        int l3 = 0;
        List<FirstClass> systemPermissions = roleOptionsTest.getSystemPermissions();
        for (FirstClass systemPermission : systemPermissions) {
            MarketRoleOptions marketRoleOptions = new MarketRoleOptions();
            marketRoleOptions.setId(systemPermission.getId());
            marketRoleOptions.setLabel(systemPermission.getLabel());
            marketRoleOptions.setPid(0);
            int insert = roleTestService.insert(marketRoleOptions);
            l1 += insert;
            List<SecondClass> secondClasses = systemPermission.getChildren();
            for (SecondClass secondClass : secondClasses) {

                MarketRoleOptions marketRoleOptions1 = new MarketRoleOptions();
                marketRoleOptions1.setId(secondClass.getId());
                marketRoleOptions1.setLabel(secondClass.getLabel());
                marketRoleOptions1.setPid(marketRoleOptions.getPrimaryKey());
                int insert1 = roleTestService.insert(marketRoleOptions1);
                l2 += insert1;
                List<ThirdClass> thirdClasses = secondClass.getChildren();
                for (ThirdClass thirdClass : thirdClasses) {
                    MarketRoleOptions marketRoleOptions2 = new MarketRoleOptions();
                    marketRoleOptions2.setId(thirdClass.getId());
                    marketRoleOptions2.setLabel(thirdClass.getLabel());
                    marketRoleOptions2.setPid(marketRoleOptions1.getPrimaryKey());
                    marketRoleOptions2.setApi(thirdClass.getApi());
                    int insert2 = roleTestService.insert(marketRoleOptions2);
                    l3 += insert2;
                }
            }

        }

        return BaseRespVo.ok(l1+l2+l3);
    }
}
